<template>
	<view class="view">
		<uni-search-bar @confirm="confirm" v-model="caketostring"  placeholder="搜索蛋糕"/>
		<view class="">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" style="width: 100%;">
				<swiper-item v-for="(item,index) in swiper" :key="index">
					<image @click="caketow(index)" :src="item.cakeImage	" mode="aspectFit"  style="width: 100%; "></image>
				</swiper-item>
				
			</swiper>
		</view>	
	<view style="margin-top: 10%;">
		<uni-grid :column="2">
			<uni-grid-item v-for="(item,index) in cakelist" :key="index">
			<view @click="caketo(index)"  style="font-size: 12px;  display: flex;flex-direction: column; justify-content: center;align-items: center;">
					<image :src="item.cakeImage" mode="widthFix" style="width: 70%; "></image>
					<text>{{item.cakeName}}</text>
					<text>购买数:{{item.cakeNumber}}</text>
					<!-- <text>购买数:{{item.}}</text> -->
					<uni-rate :max="5" :value="item.cakeScore" />
					<text style="font-weight: bold;color: #800;">${{item.cakeMoney}}</text>
			</view>
				</uni-grid-item>
	
			
		</uni-grid>
		</view>
	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiper:[],
				cakelist:[],
				caketostring:""
			}
		},
		onLoad() {
			this.getcake()
			this.getswiper()
		},
		methods: {
			caketow(index){
				uni.navigateTo({
					url: '/pages/cake/cake?id='+this.swiper[index].id,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			caketo(index){
				uni.navigateTo({
					url: '/pages/cake/cake?id='+this.cakelist[index].id,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			confirm(){
				uni.navigateTo({
					url: '/pages/search/search?name='+this.caketostring,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			getcake(){
				uni.request({
					url: this.api.findHouSex,
					method: 'GET',
					data: {},
					success: res => {
						if(res.data.code==200){
							this.cakelist=res.data.obj
							for (var i = 0; i < this.cakelist.length; i++) {
								this.cakelist[i].cakeImage="http://127.0.0.1:8989/"+this.cakelist[i].cakeImage
							}
							console.log(this.cakelist);
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			getswiper(){
				uni.request({
					url: this.api.findHou,
					method: 'GET',
					data: {},
					success: res => {
						if(res.data.code==200){
							this.swiper=res.data.obj
							for (var i = 0; i < this.swiper.length; i++) {
								this.swiper[i].cakeImage="http://127.0.0.1:8989/"+this.swiper[i].cakeImage
							}
							console.log(this.swiper);
						}
					},
					fail: () => {},
					complete: () => {}
				});
			}
		}
	}
</script>

<style>
	page,.view{
		width: 100%;
		height: 100%;
	}
</style>
